package com.jxb.commentree.service;

import com.jxb.commentree.dto.CommentDto;
import com.jxb.commentree.mapper.CommentOnMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("commentOnService")
public class CommentOnService {
    @Autowired
    private CommentOnMapper commentMapper;

    public List<CommentDto> getAllCommentByBlogId(Integer blogId) {
        List<CommentDto> commentVos = this.commentMapper.getCommentByBlogId(blogId);
        List<CommentDto> sons = getSons(commentVos);
        return sons;
    }

    private List<CommentDto> getSons(Integer parentId){
        return this.commentMapper.getCommentByParentId(parentId);
    }

    private List<CommentDto> getSons(List<CommentDto> parents){
        if(parents == null || parents.size() == 0){
            return null;
        }
        for (CommentDto parent : parents) {
            int parentId = parent.getId();
            List<CommentDto> sonCommentVos = getSons(parentId);
            //递归找子评论
            parent.setSons(getSons(sonCommentVos));
        }
        return parents;
    }

    public void addComment(CommentDto commentDto) {
        commentMapper.insertComment(commentDto);
    }
}
